package com.see.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

import com.see.servise.SEEServise;

public class SocketListener extends Thread {
	private SEEServise seeServise;
	private ServerCore serverCore;
	
	private static Logger logger = LoggerFactory.getLogger(SocketListener.class);
	
	public SocketListener(SEEServise seeServise, ServerCore serverCore) {
		this.seeServise = seeServise;
		this.serverCore = serverCore;
	}

	@Override
	public void run() {
		try {
			logger.info("Init ServerSocket");
			serverCore.serverSocket = new ServerSocket(serverCore.PORT);
			while (!Thread.interrupted() && serverCore.serverRunning) {

					Socket connection = serverCore.serverSocket.accept();
					logger.info("server: client connected");
					if (serverCore.serverRunning) {
						SocketConnection socketConnection = new SocketConnection(connection, seeServise);
						serverCore.pool.execute(socketConnection);
					} else {
						connection.close();
					}
			}
		} catch (IOException e) {
			logger.info("Error: " + e.getMessage());
		}
	}
}
